<?php

/**
 * Class Featured
 *
 * Please note:
 * Don't use the same name for class and method, as this might trigger an (unintended) __construct of the class.
 * This is really weird behaviour, but documented here: http://php.net/manual/en/language.oop5.decon.php
 *
 */
class Featured extends Controller
{
    /**
     * PAGE: index
     * This method handles what happens when you move to http://yourproject/home/index (which is the default page btw)
     */
    public function index()
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$total = $laptop_model->countLaptops();
    	
    	$pager = new Pager();
    	$pager -> initPager(0, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/pager/');
    	
    	$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit());
    	
    	$randid = rand(1,$total[0]->LAPCOUNT);    	
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	

        // debug message to show where you are, just for the demo
        // echo 'Message from Controller: You are in the controller home, using the method index()';
        require 'application/views/common/_header.php';
        require 'application/views/featured/index.php';
        require 'application/views/common/_footer.php';
    }
    
    public function search()
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$total = $laptop_model->countSearchLaptops($_POST["keywords"]);
    	 
    	$pager = new Pager();
    	$pager -> initPager(0, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/searchpager/'.$_POST["keywords"].'/'); 
    	 
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	if (isset($_POST["submit_search"])) {
    		
    		$laptops = $laptop_model->searchPaginatedLaptopList($_POST["keywords"],$pager->getStart(),$pager->getLimit());
    	}    	 
    
    	// debug message to show where you are, just for the demo
    	// echo 'Message from Controller: You are in the controller home, using the method index()';
    	require 'application/views/common/_header.php';
    	require 'application/views/featured/index.php';
    	require 'application/views/common/_footer.php';
    }
    
    public function searchpager($keywords,$page)
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$total = $laptop_model->countSearchLaptops($keywords);
    
    	$pager = new Pager();
    	$start = ($page-1) * $pager->getLimit();    	
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/searchpager/'.$keywords.'/');
    
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	$laptops = $laptop_model->searchPaginatedLaptopList($keywords,$pager->getStart(),$pager->getLimit());

    
    	// debug message to show where you are, just for the demo
    	// echo 'Message from Controller: You are in the controller home, using the method index()';
    	require 'application/views/common/_header.php';
    	require 'application/views/featured/index.php';
    	require 'application/views/common/_footer.php';
    }
    
    
    public function pager($page)
    {
    	// load a model, perform an action, pass the returned data to a variable
    	// NOTE: please write the name of the model "LikeThis"
    	$laptop_model = $this->loadModel('LaptopModel');
    	$total = $laptop_model->countLaptops();
    	 
    	$pager = new Pager();    	
    	$start = ($page-1) * $pager->getLimit();    	
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/pager/');
    	
    	$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit());    	 
    		
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);   	 
    	$sold = $laptop_model->countSold();
    
    	// debug message to show where you are, just for the demo
    	// echo 'Message from Controller: You are in the controller home, using the method index()';
    	require 'application/views/common/_header.php';
    	require 'application/views/featured/index.php';
    	require 'application/views/common/_footer.php';
    }
    
    public function brand($brand){
    	 
    	$laptop_model = $this->loadModel('LaptopModel');
    	
    	$total = $laptop_model->countLaptopsByBrand($brand);
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	$pager = new Pager();
    	$start = 0;
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/brandpager/'.$brand.'/');
    	
    	if (isset($brand)) {
    		$laptops = $laptop_model->getPaginatedLaptopListByBrand($brand,$pager->getStart(),$pager->getLimit()); 
    	} else {    		   	 
    		$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit()); 
    	}
    
    	require './application/views/common/_header.php';
    	require './application/views/featured/index.php';
    	require './application/views/common/_footer.php';
    }
    
    public function brandpager($brand,$page){
    
    	$laptop_model = $this->loadModel('LaptopModel');
    	 
    	$total = $laptop_model->countLaptopsByBrand($brand);
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	$pager = new Pager();
    	$start = ($page-1) * $pager->getLimit();
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/brandpager/'.$brand.'/');
    	
    	if (isset($brand)) {
    		$laptops = $laptop_model->getPaginatedLaptopListByBrand($brand,$pager->getStart(),$pager->getLimit()); 
    	} else {    		   	 
    		$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit()); 
    	}
    
    	require './application/views/common/_header.php';
    	require './application/views/featured/index.php';
    	require './application/views/common/_footer.php';
    }
    
    public function model($model){
    
    	$laptop_model = $this->loadModel('LaptopModel');
    	 
    	$total = $laptop_model->countLaptopsByModel($model);
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	$pager = new Pager();
    	$start = 0;
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/modelpager/'.$model.'/');
    	 
    	if (isset($model)) {
    		$laptops = $laptop_model->getPaginatedLaptopListByModel($model,$pager->getStart(),$pager->getLimit());
    	} else {
    		$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit());
    	}
    
    	require './application/views/common/_header.php';
    	require './application/views/featured/index.php';
    	require './application/views/common/_footer.php';
    }
    
    public function modelpager($model,$page){
    
    	$laptop_model = $this->loadModel('LaptopModel');
    
    	$total = $laptop_model->countLaptopsByModel($model);
    	$randid = rand(1,$total[0]->LAPCOUNT);
    	$special = $laptop_model->getLaptopById($randid);
    	$sold = $laptop_model->countSold();
    	
    	$pager = new Pager();
    	$start = ($page-1) * $pager->getLimit();
    	$pager -> initPager($start, $total[0]->LAPCOUNT);
    	$pager -> setURL(URL . 'featured/modelpager/'.$model.'/');
    	 
    	if (isset($model)) {
    		$laptops = $laptop_model->getPaginatedLaptopListByModel($model,$pager->getStart(),$pager->getLimit());
    	} else {
    		$laptops = $laptop_model->getPaginatedLaptops($pager->getStart(),$pager->getLimit());
    	}
    
    	require './application/views/common/_header.php';
    	require './application/views/featured/index.php';
    	require './application/views/common/_footer.php';
    }
}
